﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using BTCe;
using BTCe.Common;
using BTCe.Constant;
using BTCe.Data;

namespace BTCe.Data
{
    public class BTCeController
    {


        BTCeAccessInfo _AccessInfo;
        PublicExchange _PublicExchange;
        OrderExchange _OrderExchange;
        WalletComm _WalletComm;
        //WalletInfo _Wallet;

        DataSet _dsInformation;
        DataTable _dtBidTable;
        DataTable _dtAskTable;
        DataTable _dtOpenOrderTable;




        public BTCeController()
        {
            _AccessInfo = new BTCeAccessInfo(eBTCeServer.RUNNING);

            initialized();
        }

        public void initialized()
        {
            _PublicExchange = new PublicExchange(_AccessInfo);
            _WalletComm = new WalletComm(_AccessInfo);
            _OrderExchange = new OrderExchange(_AccessInfo);
            //_Wallet = new WalletInfo();

        }




        /// <summary>
        /// Logins this instance.
        /// </summary>
        /// <returns></returns>
        public bool Login()
        {
            bool bReturn;

            bReturn = false;

            //기존에 로그인이 되어 있는지 확인한다.
            if (lsLogin())
                return true;

            bReturn = _AccessInfo.Login();

            //로그인 되면 정보를 갱신한다.
            initialized();
            
            return bReturn;
        }

        /// <summary>
        /// Refreshes the token.
        /// </summary>
        /// <returns></returns>
        public bool RefreshToken()
        {
            bool bReturn;

            bReturn = false;

            //기존에 로그인이 되어 있는지 확인한다.
            if (lsLogin())
                return true;

            bReturn = _AccessInfo.RefreshToken();

            return bReturn;
        }

        /// <summary>
        /// 로그인 되어 있는지 확인한다.
        /// </summary>
        /// <returns></returns>
        public bool lsLogin()
        {
            return _AccessInfo.IsLogin();
        }


        private bool RetrieveWallet()
        {
            bool bReturn = false;

            if (!lsLogin())
                return bReturn;

            _WalletComm.RetrieveWallet();

            bReturn = true;

            return bReturn;
        }

        public WalletInfo GetWalletInfo()
        {
            _WalletComm.RetrieveWallet();

            return _WalletComm.GetWalletInfo();
        }

        public WalletDeposit GetWalletDeposit()
        {
            _WalletComm.RetrieveWallet();

            return _WalletComm.GetWalletDeposit();
        }

        public void RetreivedContract()
        {
            _PublicExchange.RetreivedContract();
        }

        public DataTable GetAskTable()
        {
            return _PublicExchange.GetAskTable();
        }

        public DataTable GetBidTable()
        {
            return _PublicExchange.GetBidTable();
        }

        public void RetreivedOpenOrder()
        {
            _PublicExchange.RetreivedOpenOrder();
        }

        public DataTable GetOpenOrderTable()
        {
            return _PublicExchange.GetOpenOrderTable();
        }

        public void OrderBuyMarket(string strKRWVolume)
        {
        }

        public void OrderBuyLimit(string strKRWPrice, string strBTCVolume)
        {
            if (!CommonFunction.IsNumeric(strKRWPrice))
            {
                return;
            }
            else if (!CommonFunction.IsNumeric(strBTCVolume))
            {
                return;
            }


            _OrderExchange.OrderBuy(strKRWPrice, strBTCVolume);
        }

        public void OrderSellMarket(string strBTCVolume)
        {
        }

        public void OrderSellLimit(string strKRWPrice, string strBTCVolume)
        {
            if (!CommonFunction.IsNumeric(strKRWPrice))
            {
                return;
            }
            else if (!CommonFunction.IsNumeric(strBTCVolume))
            {
                return;
            }

            _OrderExchange.OrderSell(strKRWPrice, strBTCVolume);
        }

        public void OrderCancel(string strOrderID)
        {
            if (!CommonFunction.IsNumeric(strOrderID))
            {
                return;
            }

            _OrderExchange.OrderCancel(strOrderID);
        }
        
        public void RefreshData()
        {
            RetreivedContract();

            RetreivedOpenOrder();

            RetrieveWallet();
        }

        public string BalanceBTC()
        {
            return _WalletComm.GetWalletDeposit().Total_BTC.ToString();
        }

        public string BalanceCurrency()
        {
            return _WalletComm.GetWalletDeposit().Total_USD.ToString();
        }
    }
}
